#!/usr/bin/python -tt
import re
import logging

def same_text(str1, str2):
	return str1.lower() == str2.lower()

def has_attr(a, attr, val):
	for item in a:
		if item[0].lower() == attr.lower() and re.search(val, item[1].lower(), re.IGNORECASE):
			return True
	return False

def get_attr(a, attr):
	for item in a:
		if item[0].lower() == attr.lower():
			return item[1]
	return ''

from HTMLParser import HTMLParser
from htmlentitydefs import name2codepoint
import locale

# create a subclass and override the handler methods
class MyHTMLParser(HTMLParser):
	found_table1 = False
	found_table = False
	inside_row = False
	found_tagname = False
	tag_type = ''
	row_start = False
	first_col = False
	tag_count = ''
	parsedCount = 0
	write_proc = None
	tag_name = ''
	def handle_starttag(self, tag, attrs):
#		print "Encountered a start tag:", tag
		if not self.found_table1 and same_text(tag, 'th') and \
		  has_attr(attrs, 'width', '8%'):
			self.found_table1 = True
			print '-------------------------------------------'
		if self.found_table and has_attr(attrs, 'class', "tag-type-.+"):
			self.found_tagname = True
			self.tag_type = get_attr(attrs, 'class')[9:].strip()
		if self.found_table and same_text(tag, 'tr'):
			self.row_start = True
		if self.row_start and same_text(tag, 'td'):
			self.first_col = True
			self.row_start = False

	def handle_endtag(self, tag):
#		print "Encountered an end tag :", tag
				
		if same_text(tag, 'table'):
			self.found_table = False
			logging.debug('^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^')
		if self.found_tagname and same_text(tag, 'span'):
			if self.tag_name:
				self.parsedCount = self.parsedCount + 1
				if self.write_proc:
					self.write_proc(self.tag_count, self.tag_type, self.tag_name)
			self.found_tagname = False
			self.tag_type = ''
			self.tag_name = ''
		if self.first_col and same_text(tag, 'td'):
			self.first_col = False

	def handle_data(self, data):
#		print "Encountered some data  :", data
		if type(data) is str:
			data = unicode(data, locale.getpreferredencoding())
		if not self.found_table and self.found_table1 and \
		  re.search('Posts', data, re.IGNORECASE):
			self.found_table = True
			print '==================================================='
		if self.first_col and self.found_tagname:
			raise Exception('Invalid input html, first column has tag name')
		if self.found_tagname:
			self.tag_name = self.tag_name + data.strip()
		if self.first_col:
			self.tag_count = data.strip()
#			print 'Found tag count', data

	def handle_entityref(self, name):
		c = unichr(name2codepoint[name])
		#print "Named ent:", c
		self.handle_data(c)
		
	def handle_charref(self, name):
		if name.startswith('x'):
			c = unichr(int(name[1:], 16))
		else:
			c = unichr(int(name))
		#print "Num ent  :", c
		self.handle_data(c)
		
		
def do_parse_page(pageurl, write_proc):
	# instantiate the parser and fed it some HTML
	logging.debug( 'Parsing ' + pageurl)
	parser = MyHTMLParser()
	parser.write_proc = write_proc
	import urllib2
	response = urllib2.urlopen(pageurl)
	html = response.read()
	parser.feed(html)
	return parser.parsedCount

if __name__ == '__main__':
	import sys
	if len(sys.argv) > 2:
		raise Exception('Should pass page URL')
	pageurl = sys.argv[1]
	print 'Fetching', pageurl
	#pageurl = 'http://gelbooru.com/index.php?page=tags&s=list&pid=240900'
	print 'Parsed %d tags' % (do_parse_page(pageurl))
	